package com.herui.ota.service.iml;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.herui.ota.dao.mysql.UserMapper;
import com.herui.ota.entity.User;
import com.herui.ota.enums.ResponseEnum;
import com.herui.ota.exception.ServiceException;
import com.herui.ota.pojo.form.UserFilterForm;
import com.herui.ota.pojo.form.WxUserForm;
import com.herui.ota.pojo.vo.UserVo;
import com.herui.ota.service.UserService;
import com.mlnx.common.form.PageForm;
import me.chanjar.weixin.common.error.WxErrorException;
import org.shan.base.layer.user.entity.Role;
import org.shan.base.layer.user.entity.UserAccount;
import org.shan.base.layer.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * author name: amanda.shan
 * create time: 2020-05-19 13:13:54
 */
@Service
public class UserServiceIml extends BaseService<UserMapper, User> implements UserService {

    @Autowired
    private LightUserAccountServiceIml userAccountService;
    @Autowired
    RoleService roleService;

    @Autowired
    @Override
    public void setBaseMapper(UserMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Override
    public IPage page(PageForm pageForm, Object filter) throws IllegalAccessException {

        Page<UserVo> userVoPage = baseMapper.listUserVo(new Page<>(pageForm.getCurrent(), pageForm.getSize()),
                (UserFilterForm) filter);
        userVoPage.getRecords().stream().forEach(userAdminVo -> userAdminVo.setLock(lightUserAccountServiceIml.isLock(userAdminVo.getId())));

        return userVoPage;
    }

    @Override
    public User findByUsername(String username) {
        return baseMapper.selectOne(new QueryWrapper<User>().eq("user_name", username));
    }


    @Override
    public UserVo getUserInfo() {

        User user = getById(getUserId());
        UserVo userVo = new UserVo();
        BeanUtil.copyProperties(user, userVo);

        return userVo;
    }

    @Override
    public Integer getUserSize() {
        return  count(new QueryWrapper<User>());
    }


    @Override
    public boolean updateById(User entity) {
        return super.updateById(entity);
    }


    public static void main(String[] args) {
        long l = 24 * 60 * 60 * 1000;
        l = System.currentTimeMillis() + l * 365;
        System.out.println(30 * 24 * 60 * 60 * 1000);
        System.out.println(l);
        System.out.println(new Date(l));
    }
}

